package com.amigo.online.provider.merchant.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import com.amigo.online.common.core.mybatis.MyMapper;
import com.amigo.online.common.param.result.goods.GoodsCalculationInfo;
import com.amigo.online.common.param.result.goods.GoodsDetailInfo;
import com.amigo.online.common.param.result.goods.HomeGoodsInfo;
import com.amigo.online.common.param.result.goods.MerchantGoodsInfo;
import com.amigo.online.provider.merchant.model.entity.Goods;

@Mapper
@Component
public interface GoodsMapper extends MyMapper<Goods> {

    /**
     * @Title: findHotGoods @Description: (这里用一句话描述这个方法的作用) @param: @param
     * label @param: @param category @param: @param service @param: @param
     * minPrice @param: @param maxPrice @return: void @throws
     */
    List<HomeGoodsInfo> findHotGoods(@Param("label") Integer label, @Param("category") Integer category,
                                     @Param("service") Integer service, @Param("minPrice") Integer minPrice, @Param("maxPrice") Integer maxPrice,
                                     @Param("start") int start, @Param("number") int number);

    /**
     * @Title: findRecommendGoods @Description: (这里用一句话描述这个方法的作用) @param: @param
     * label @param: @param category @param: @param service @param: @param
     * minPrice @param: @param maxPrice @param: @return @return:
     * List<HomeGoodsInfo> @throws
     */
    List<HomeGoodsInfo> findRecommendGoods(@Param("label") Integer label, @Param("category") Integer category,
                                           @Param("service") Integer service, @Param("minPrice") Integer minPrice, @Param("maxPrice") Integer maxPrice,
                                           @Param("start") int start, @Param("number") int number);

    List<Goods> getMerchantCollectList(@Param("userId") Long userId, @Param("start") Integer start,
                                       @Param("size") Integer size);

    @Update("update goods_tbl set feedback = #{newFeedback},grade = #{grade} where id = #{goodsId}")
    int updateMerchantById(@Param("goodsId") Long goodsId, @Param("newFeedback") Integer newFeedback,
                           @Param("grade") Double grade);

    GoodsDetailInfo getGoodsDetailById(Long goodsId);

    @Update("update goods_tbl set feedback = #{newFeedback} where id = #{goodsId}")
    int updateFeedbackById(@Param("goodsId") Long goodsId, @Param("newFeedback") Integer newFeedback);

    List<HomeGoodsInfo> getGoodsOrderBYField(@Param("merchantId") Long merchantId, @Param("start") Integer start,
                                             @Param("size") Integer size, @Param("type") Integer type, @Param("categoryId") Long categoryId);

    /**
     * @Title: shelfMerchantGoodsByGoodsId @Description:
     * (这里用一句话描述这个方法的作用) @param: @param goodsId @return: void @throws
     */
    @Update("update goods_tbl set status = 2, up_time = unix_timestamp() where id = #{goodsId}")
    int shelfMerchantGoodsByGoodsId(@Param("goodsId") long goodsId);

    /**
     * @Title: checkGoodsIsMerchantGoods @Description:
     * (这里用一句话描述这个方法的作用) @param: @param merchantId @param: @param
     * goodsId @param: @return @return: Long @throws
     */
    @Select("select id from goods_tbl where merchant_id = #{merchantId} and id = #{goodsId} and deleted = 0")
    Long checkGoodsIsMerchantGoods(@Param("merchantId") long merchantId, @Param("goodsId") long goodsId);

    @Update("update goods_tbl set status = #{goodsStatusShelf} where id = #{goodsId}")
    int updateMerchantGoodsStatusByGoodsId(long goodsId, int goodsStatusShelf);

    @Update("update goods_tbl set status = 3, down_time = unix_timestamp() where id = #{goodsId}")
    int obtainedMerchantGoodsByGoodsId(@Param("goodsId") long goodsId);

    @Update("update goods_tbl set deleted = 1, deleted_time = unix_timestamp() where id = #{goodsId}")
    int deleteMerchantGoodsByGoodsId(@Param("goodsId") long goodsId);


    List<MerchantGoodsInfo> getGoods(@Param("merchantId") Long merchantId, @Param("status") Integer status,
                                     @Param("categoryId") Long categoryId, @Param("start") Integer start, @Param("size") Integer size,
                                     @Param("name") String language);

    Integer getGoodsCount(@Param("merchantId") Long merchantId, @Param("status") Integer status,
                          @Param("categoryId") Long categoryId);

    @Select("select id from goods_tbl order by id desc limit 1")
    int findMaxGoodsId();

    @Select("select id from goods_tbl where id >= #{startId} and id <= #{endId} and deleted = 0 and status = 3")
    List<GoodsCalculationInfo> listWeightCalculateGoods(@Param("startId") int start, @Param("endId") int endId);

    @Update("<script>"
            + " <foreach collection='updateMap.entrySet()' index= 'key' item='value' open='' close='' separator=';'>"
            + "	update goods_tbl set hot_weight = #{value} where id = #{key}"
            + "	</foreach>"
            + "</script>")
    void multiUpdateGoods(@Param("updateMap") Map<Long, Double> weightMap);

    @Select("select count(*) from goods_tbl g left join merchant_tbl mer on g.merchant_id = mer.id where g.id = #{goodsId} and mer.user_id =#{userId}")
    int checkGoodsIsUserHas(@Param("goodsId") Long goodsId, @Param("userId") Long userId);

    @Select("select count(*) from goods_comment_tbl where user_id = #{userId} and goods_id = #{goodsId} and deleted = 0")
    int checkGoodsIsCommented(@Param("goodsId") Long goodsId, @Param("userId") Long userId);

}